<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>使用原子组的别名优化正则</title>
  </head>
  <body>
    <main>
      <a id="hd" href="https://www.houdunren.com">后盾人</a>
      <a href="https://www.hdcms.com">hdcms</a>
      <a href="https://www.sina.com.cn">新浪</a>
    </main>
  </body>
  <script>
    //[{link:"",title:""}]
    const main = document.querySelector("body main");
    //原子组\1\2用在正则中
    //原子组$1$2用在替换中
    //当原子组太多，不好区分时，可以取别名
    //原子组别名  (?<link>) 在groups属性中
    const reg = /<a.*?href=(['"])(?<link>.*?)\1>(?<title>.*?)<\/a>/gi;
    // console.log(main.innerHTML.match(reg));
    const links = [];
    for (const iterator of main.innerHTML.matchAll(reg)) {
      //   console.log(iterator["groups"]);
      links.push(iterator["groups"]);
    }
    console.log(links);
  </script>
</html>
